import path from "@/framework/util/path.ts";
import type { LocalVersionItem } from "@/modules/dev-env/common/types.ts";

export interface GenMariadbConfigOptions {
  /**
   * 数据目录
   */
  datadir?: string;
}

/**
 * 生成my.ini默认配置
 * @param version 版本信息
 * @param opts 配置项
 */
export function getMariadbDefaultConfig(version: LocalVersionItem, opts?: GenMariadbConfigOptions): string {
  return `[mysqld]
# 服务端口
port = 3306
# MySQL根目录
basedir=${version.installPath}
# 数据目录
datadir=${opts?.datadir ?? path.join(version.installPath, "data")}
# 最大连接数
# max_connections=200
# 默认存储引擎
default-storage-engine=INNODB
# 服务器默认编码
character-set-server=utf8mb4
# 服务器默认字符集
collation_server=utf8mb4_unicode_ci
# 大小写
#lower_case_table_names=1
# 跳过安全检查
#skip-grant-tables

[mysql]
default-character-set=utf8mb4

[client]
plugin-dir=${path.join(version.installPath, "lib/plugin")}
`;
}

export const MySQLCodes = {
  SetDefaultPwd: `-- 修改默认密码
ALTER USER 'root'@'localhost' IDENTIFIED BY 'new pwd';
-- 开启远程访问权限
UPDATE mysql.user SET Host='%' WHERE User='root' AND Host='localhost';
-- 刷新
FLUSH PRIVILEGES;`,
};

export const MySQLCodesLines = MySQLCodes.SetDefaultPwd.split("\n").length;
